home *** CD-ROM | disk | FTP | other *** search
- /*
- File: FontHandlerVariations.c
-
- Contains: QuickDraw GX to PostScript conversion code.
- File contains code to deal with font variation snapshots
-
- Version: Technology: Quickdraw GX 1.1.x
-
- Copyright: © 1991-1997 by Apple Computer, Inc., all rights reserved.
- */
-
- #include <Collections.h>
- #include <MacMemory.h>
- #include <GXExceptions.h>
- #include "GXToPSBuildConfig.h"
- #include "GXPrintingUniverse.h"
- #include <Collections.h>
- #include "GXGraphics.h"
- #include "FontDatabase.h"
- #include "FontHandler.h"
- #include "FontHandlerPrivate.h"
- #include "FontHandlerVariations.h"
-
- #ifdef resumeLabel
- #undef resumeLabel
- #endif
- #define resumeLabel(exception)
-
-
- /****************************
- FDBEqualVariations:
-
- Test two varations for equality
- Assumes both variations have same
- number of axes.
-
- num: number of axes
- v1: pointer to 1st coordinates
- v2: pointer to 2nd coordinates
-
- ******************************/
- Boolean FHEqualVariations(long num, gxFontVariation* v1, gxFontVariation* v2)
- {
- register short idx;
-
- for (idx = num - 1; idx >= 0; --idx) {
-
- if ( (v1->name != v2->name) || (v1->value != v2->value))
- return(false);
-
- ++v1;
- ++v2;
-
- }//end for
-
- return(true);
-
- }//FHEqualVariations
-
-
-
-
- //<FF>
- /**********************************
-
- FHCountSnapshots:
-
- Function counts the number of snapshot
- fonts for the document.
-
- ************************************/
- long FHCountSnapshots(TFontHandlerHdl hFHRec)
- {
- long n;
-
- n = CountCollectionItems((*hFHRec)->snapShots); // how many items in snapshot collection.
-
- return(n);
-
- }//FHCountSnapshots
-
-
-
- /***********************************
-
- FHGetIndexedSnapshot:
-
- Retrieves the snapshot id by index in the collection.
-
- ************************************/
- OSErr FHGetIndexedSnapshot(TFontHandlerHdl hFHRec, long index, fhFont *theFont)
- {
- OSErr status;
-
- status = GetIndexedCollectionItemInfo((*hFHRec)->snapShots,
- index,
- nil, // We don't care about the real font reference.
- (long*)theFont, // the snapshot is cached in the id in addition to the data.
- nil,
- nil);
-
- ncheck(status);
- return(status);
-
- }//FHGetIndexedSnapshot
-
-
-
- //<FF>
- /*************************************
-
- Function: FHAddFontSnapShot
-
- Function adds a snapshot to the snapshot collection.
- Returns the snapshot id.
-
- hFHRec: the font handler context handle
- theFont: the font in question.
- dbIndex: index of bit array in database.
- snapshotID: The snapshot id to use.
-
- ***************************************/
- OSErr FHAddFontSnapShot(TFontHandlerHdl hFHRec, gxFont theFont, long dbIndex, fhFont *snapshotID)
- {
- OSErr status = noErr;
- TFontHandlerPtr pFHRec;
- TFHSnapShot snapShotRec;
- Collection snapShots; // the collection of snapshots.
- long nSnapshots, idx;
-
- pFHRec = *hFHRec;
-
- snapShots = pFHRec->snapShots;
-
- /** Make sure it is not already in the database **/
-
- nSnapshots = CountTaggedCollectionItems(snapShots, (CollectionTag)theFont);
-
- for (idx = 1; idx <= nSnapshots; ++idx) {
-
- status = GetTaggedCollectionItem(snapShots, (CollectionTag)theFont, idx, nil, &snapShotRec);
- nrequire(status, failed_GetItem);
-
- if (snapShotRec.dbIndex == dbIndex) {
-
- *snapshotID = snapShotRec.snapShotFont;
- break;
-
- }//end if
-
- }//end for
-
-
- /* If it wasn't already in the collection, add it */
-
- if (idx > nSnapshots) {
-
- *snapshotID = (fhFont)(*hFHRec)->nextSnapShot;
- snapShotRec.snapShotFont = *snapshotID;
- snapShotRec.dbIndex = dbIndex;
- snapShotRec.theFont = theFont;
-
- status = AddCollectionItem(snapShots,
- (CollectionTag)theFont, // the snapshot id will be collection tag
- (long)*snapshotID, // the gx font will be the ID
- sizeof(TFHSnapShot), &snapShotRec);
- nrequire(status, failed_AddItem);
-
- (*hFHRec)->nextSnapShot += 1; // increment the next available snapshot id.
-
- }//end if
-
- failed_AddItem:
- failed_GetItem:
-
- return(status);
-
- }//FHAddFontSnapShot
-
-
-
- //<FF>
- /****************************************
-
- Function: FHGetSnapShotFont
-
- Function returns the real font ID from a snapshot font id.
-
- hFHRec: The font handler context handle.
- snapShotFont: snap shot font to look up.
- dbIndex: if not nil, the variation/bit pair index is returned here.
-
- the gxFont ID is reuturned, nil means snapshot is invalid.
-
- ******************************************/
- gxFont FHGetSnapShotFont(TFontHandlerHdl hFHRec, fhFont snapShotFont, long *dbIndex)
- {
- long idx;
- gxFont theFont;
- TFHSnapShot snapshotRec;
- long n;
- Collection snapshots;
- fhFont theSnapshot;
-
- snapshots = (*hFHRec)->snapShots;
- n = CountCollectionItems(snapshots);
-
- /** The snapshot ID is stored as the id in the collection item, search for a match **/
-
- for (idx = 1; idx <= n; ++idx) {
-
- GetIndexedCollectionItemInfo(snapshots, idx,
- (CollectionTag*)&theFont, // the collection tag is the real font
- (long*)&theSnapshot, // The id is the snapshot ID
- nil, nil);
-
- if (theSnapshot == snapShotFont)
- break;
-
- }//end for
-
- if (idx > n) {
-
- theFont = nil; // couldn't find a match.
-
- } else if (dbIndex != nil) {
-
- GetIndexedCollectionItem(snapshots, idx, nil, &snapshotRec);
- *dbIndex = snapshotRec.dbIndex;
-
- }//end if
-
- return(theFont);
-
- }//FHGetSnapShotFont
-
-
- //<FF>
- /********************************************
-
- Function: FontHandlerGetStyleFont
-
- Function is wrapper for real call. Returns an fhFont
- instead of gxFont
-
- context: A font handler context.
- theStyle: The style to retrieve the snapshot font from.
- theFont: (returned) This is the font snapshot ID
-
- **********************************************/
- OSErr _FontHandlerGetStyleFont(TFontHandlerContext context, gxStyle theStyle, fhFont *theFont)
- {
- OSErr status = noErr;
- long nVariations; // Number of variations for the style.
- gxFontVariation *theVariations;
- gxFontVariation *dbVariations;
- long nSnapshots; // Number of snapshots in the database.
- long idx;
- TFHSnapShot snapShotRec;
- TFontDbase fontDbase;
- Collection snapShots;
- TFontHandlerPtr pFHRec;
- Handle h = nil;
- gxFont theGXFont;
-
- pFHRec = *(TFontHandlerHdl)context;
- fontDbase = pFHRec->docDbase;
- snapShots = pFHRec->snapShots;
-
- theGXFont = FHgxGetStyleFont(theStyle);
-
- nSnapshots = CountTaggedCollectionItems(snapShots, (CollectionTag)theGXFont);
-
- if (nSnapshots > 0) {
-
- /* Get the variations from the style */
-
- nVariations = GXGetStyleFontVariationSuite(theStyle, nil);
-
- if (nVariations > 0) {
-
- status = FHSetWorkHandleSize((TFontHandlerHdl)context, nVariations * sizeof(gxFontVariation), 5, &h);
- nrequire(status, failed_workhandle);
- HLock(h);
- theVariations = (gxFontVariation*)*h;
- GXGetStyleFontVariationSuite(theStyle, theVariations);
-
- }//end if
-
- /* Now find a match for the variations in the database */
-
- for (idx = 1; idx <= nSnapshots; ++idx) {
-
- status = GetTaggedCollectionItem(snapShots, (CollectionTag)theGXFont, idx, nil, &snapShotRec);
- nrequire(status, failed_getitem);
-
- /** If the font doesn't need snapshots then return the snapshot for the main bit array **/
- if (snapShotRec.dbIndex == eMainBits) {
-
- /* The only way we could have a snapshot with dbIndex == eMainBits is if font doesn't require snapshots */
- *theFont = snapShotRec.snapShotFont;
- break;
-
- } else {
-
- /* Compare the style's variations against the ones in the font database for this snapshot */
-
- status = FontDbaseGetGlyphBits(fontDbase, theGXFont, snapShotRec.dbIndex, nil, &dbVariations);
- nrequire(status, failed_getbits);
-
- /* warning, the font database is not locked so memory better not move during the comparison */
-
- if (FHEqualVariations(nVariations, dbVariations, theVariations)) {
-
- *theFont = snapShotRec.snapShotFont;
- break;
-
- }//end if
-
- }//end if
-
- }//end for
-
- } else {
-
- /** If there were no entries matching the style's gxFont and variations, return nil for the fhFont **/
-
- *theFont = nil;
- status = noErr;
-
- }//end if
-
- failed_getbits:
- failed_getitem:
-
- if (h != nil)
- FHReleaseWorkspace((TFontHandlerHdl)context, 5);
-
- failed_workhandle:
-
- check(*theFont); /* should only happen if font/variation combo was not in database (extensions can cause this) */
-
- return(status);
-
- }//FontHandlerGetStyleFont
-
-
-
- //<FF>
- /**************************************************
-
- Function: FontHandlerGetStyleFontVariations
-
- Function returns the variations from a style
- based on the snapshot that the style uses.
-
- If the snapshot uses the main bit array, thus
- meaning the gxFont did not require snapshots,
- then the variations are returned.
-
- Otherwise, no variations are returned
-
- context: A font handler context.
- theStyle: The style to retrieve the snapshot font from.
- count: (returned) The number of axes.
- variations: (returned) The variation data.
-
- ****************************************************/
-
- OSErr _FontHandlerGetStyleFontVariations(TFontHandlerContext context, gxStyle theStyle, long *count, gxFontVariation variations[])
- {
- OSErr status;
- fhFont theSnapShot;
- gxFont theFont;
- TFHSnapShot snapShotRec;
-
- /* Get the snapshot id */
-
- status = _FontHandlerGetStyleFont(context, theStyle, &theSnapShot);
- nrequire(status, failed_snapshot);
-
- /* Get the snapshot data */
-
- theFont = FHgxGetStyleFont(theStyle); // the real font helps us get into the collection without searching.
-
- status = GetCollectionItem( (*(TFontHandlerHdl)context)->snapShots,
- (CollectionTag)theFont,
- (long)theSnapShot,
- nil,
- &snapShotRec);
- nrequire(status, failed_GetItem);
-
- /******
- If the snapshot points to the main bits, then that means font
- does not require snapshots, return real variation info from style
- ******/
- if (snapShotRec.dbIndex == eMainBits) {
-
- *count = GXGetStyleFontVariationSuite(theStyle, variations);
-
- } else {
-
- /* if the style requires a real snapshot, then to the callers point of view there are no variations */
-
- *count = 0;
-
- }//end if
-
- failed_GetItem:
- #if DEBUGLEVEL > 1
- if (status == collectionItemNotFoundErr)
- dprintf(notrace, "Wierd, asking for variations that are not in database. Shouldn't happen");
-
- #endif
-
- failed_snapshot:
-
- return(status);
-
- }//FontHandlerGetStyleVariations
-